package com.ssm.server.feign.builder;

import com.ssm.server.feign.annotaion.MyFeignClient;
import com.ssm.server.feign.builder.target.DynamicUrlFeignTarget;
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class MyFeignBuilder {

    private final static Logger log = LoggerFactory.getLogger(MyFeignBuilder.class) ;

    public static <T>T getFeign(Class<T> apiType ,String url  ,String serviceName ,String rootPath){
        return Feign.builder().decoder(new JacksonDecoder()).encoder(new JacksonEncoder())
                .options(new Request.Options(15*1000,60*1000))
                .retryer(Retryer.NEVER_RETRY)
                .target(new DynamicUrlFeignTarget<T>(serviceName,url,apiType,rootPath));
    }

    public static <T> T createFeign(Class<T> apiType){
        log.info("init---feign---{}",apiType.getName());
        MyFeignClient mf = apiType.getAnnotation(MyFeignClient.class);
        if(Objects.isNull(mf)){
            throw new RuntimeException("获取FeignClient异常当前"+apiType.getName()+"未被MyFeignClient注解标记");
        }
        return getFeign(apiType,mf.url(), mf.name(), mf.rootPath());
    }
}
